package com.ali.common;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import java.nio.charset.StandardCharsets;

public class HexUtil {
    /**
     * Hex加密
     */
    public static String encrypt(String content) {
        byte[] contentByte = content.getBytes(StandardCharsets.UTF_8);
        return Hex.encodeHexString(contentByte);
    }

    /**
     * Hex加密
     */
    public static String encrypt(byte[] contentByte) {
        return Hex.encodeHexString(contentByte);
    }

    /**
     * Hex解密
     */
    public static String getStringByDecrypt(String encryptStr) {
        try {
            byte[] encryptStrByte = Hex.decodeHex(encryptStr);
            return new String(encryptStrByte, StandardCharsets.UTF_8);
        } catch (DecoderException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Hex解密
     */
    public static byte[] getByteByDecrypt(String encryptStr) {
        try {
            return Hex.decodeHex(encryptStr);
        } catch (DecoderException e) {
            throw new RuntimeException(e);
        }
    }
}